package org.darcy.controller.org;

import java.util.List;

import org.darcy.controller.BaseController;
import org.darcy.entity.org.Company;
import org.darcy.entity.org.Department;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.framework.model.R;
import org.darcy.service.org.OrgDepartmentService;
import org.darcy.utils.common.ReturnCode;
import org.darcy.utils.vo.ReturnVo;
import org.darcy.vo.CheckExistVo;
import org.darcy.vo.org.OrgTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Tag(name="DepartmentController接口：组织架构信息")
@RestController
@RequestMapping("/org/department")
@Slf4j
public class DepartmentController extends BaseController<Department> {
	
	private final String info_prefix = "[组织架构模块]";

	@Autowired
	private OrgDepartmentService service;
	
	@Operation(summary = "组织架构列表")
	@PostMapping(value = "list")
	public R list(@RequestBody PageParam<Department> param) {
		log.debug("{}-列表数据", info_prefix);
		PageData<Department> data = service.page(param);
		return R.ok().data(data);
	}

	@Operation(summary = "企业详情")
	@GetMapping("/view/{id}")
	public R view(@PathVariable Integer id) {
		log.debug("{}-查看组织架构", info_prefix);
		Department data = service.getById(id);
		return R.ok().data(data);
	}

	@Operation(summary = "新增组织架构")
	@PostMapping("add")
	public R add(@RequestBody Department record) {
		log.debug("{}-新增组织架构: {}", info_prefix, record);
		service.insert(record);
		return R.ok("新增成功").data(record);
	}

	@Operation(summary = "根据ID删除组织架构")
	@PostMapping("/del/{id}")
	public R del(@PathVariable Integer id) {
		log.debug("{}-根据ID删除", info_prefix, id);
		service.delete(id);
		return R.ok("删除成功");
	}

	@Operation(summary = "更新组织架构")
	@PostMapping("update")
	public R update(@RequestBody Department record) {
		log.debug("{}-保存信息: {}", info_prefix, record);
		service.update(record);
		return R.ok("更新成功");
	}

	@Operation(summary = "查看属性")
	@GetMapping("/model")
	public Company model() {
		return null;
	}



	/**
	 * 判断部门是否存在
	 *
	 * @param checkExistVo 参数
	 * @return
	 */
	@PostMapping(value = "/checkEntityExist", produces = "application/json")
	protected R checkEntityExist(@RequestBody CheckExistVo checkExistVo) {
		return this.checkExist(service, checkExistVo);
	}

	/**
	 * 获取组织树
	 *
	 * @return
	 */
	@GetMapping(value = "/getOrgTree", produces = "application/json")
	public ReturnVo<List> getOrgTree() {
		ReturnVo<List> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
		List<OrgTreeVo> orgTreeVos = service.getOrgTree();
		returnVo.setData(orgTreeVos);
		return returnVo;
	}

	/**
	 * 通过公司id获取部门列表
	 *
	 * @return
	 */
	@GetMapping(value = "/getDepartmentByCompanyId/{companyId}", produces = "application/json")
	public ReturnVo<List> getDepartmentByCompanyId(@PathVariable String companyId) {
		ReturnVo<List> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
		LambdaQueryWrapper<Department> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
		departmentLambdaQueryWrapper.eq(Department::getCompanyId, companyId).eq(Department::getDelFlag, 1);
		List<Department> departments = service.getList(departmentLambdaQueryWrapper);
		returnVo.setData(departments);
		return returnVo;
	}


	/**
	 * 根据条件获取 - 不分页
	 *
	 * @param department 查询参数
	 * @return
	 */
	@PostMapping(value = "/getAll", produces = "application/json")
	public ReturnVo<List<Department>> getAll(@RequestBody Department department) {
		ReturnVo<List<Department>> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
		List<Department> list = service.getAll(department);
		returnVo.setData(list);
		return returnVo;
	}

	/**
	 * 添加或者修改
	 *
	 * @param department 参数
	 * @return
	 */
	@PostMapping(value = "/saveOrUpdate", produces = "application/json")
	public ReturnVo<String> saveOrUpdate(@RequestBody Department department) {
		ReturnVo<String> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
		service.saveOrUpdate(department, this.getLoginUser());
		return returnVo;
	}

	/**
	 * 删除
	 *
	 * @param ids
	 * @return
	 */
	@PostMapping(value = "/delete", produces = "application/json")
	public ReturnVo<String> delete(@RequestBody List<String> ids) {
		ReturnVo<String> returnVo = service.deleteByIds(ids);
		return returnVo;
	}

}
